package cn.jyc.ssm01.ctrl;

import cn.jyc.ssm01.dao.BillsMapper;
import cn.jyc.ssm01.pojo.BType;
import cn.jyc.ssm01.pojo.Bills;
import cn.jyc.ssm01.service.BillService;
import cn.jyc.ssm01.utils.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.time.LocalTime;
import java.util.Date;
import java.util.List;

@Controller
public class BillsCtrl {

    @Autowired
    private BillService bs;

    @RequestMapping("/main")
    public String main(@RequestParam(value = "a_type", required = false) String type,
                       @RequestParam(value = "a_date1", required = false) String d1,
                       @RequestParam(value = "a_date2", required = false) String d2,
                       @RequestParam(value = "a_amo1", required = false) String a1,
                       @RequestParam(value = "a_amo2", required = false) String a2,
                       @RequestParam(value = "sx", required = false) String sx,
                       HttpSession sess) {
        System.out.println(Thread.currentThread().getId() + "-" + LocalTime.now() + "：" + type + "、" + d1 + "、" + d2 + "、" + a1 + "、" + a2 + "、" + sx);

        sess.setAttribute("type", type);
        sess.setAttribute("d1", d1);
        sess.setAttribute("d2", d2);
        sess.setAttribute("a1", a1);
        sess.setAttribute("a2", a2);

        if (type != null) {
            System.out.println(Thread.currentThread().getId() + "-" + LocalTime.now() + "-数据类型：" + type.getClass() + "、" + d1.getClass() + "、" + d2.getClass() + "、" + a1.getClass() + "、" + a2.getClass());
        }
        List<BType> ts = bs.show();
        sess.setAttribute("types", ts);

        Integer type2 = type == null || type.equals("") ? null : Integer.valueOf(type);
//        Date d12 = d1 == null || d1.equals("") ? null : DateUtil.stringToDate(d1);
//        Date d22 = d2 == null || d2.equals("") ? null : DateUtil.stringToDate(d2);
        Double a12 = (a1 == null) || a1.equals("") ? null : Double.valueOf(a1);
        Double a22 = a2 == null || a2.equals("") ? null : Double.valueOf(a2);

        Integer iii = 1;
        PageInfo<Bills> pi = (PageInfo<Bills>) sess.getAttribute("pageinfo");
        if (pi != null) {
            iii=pi.getPageNum();
            if (sx.equals("1")&&pi.getPages()>iii) {
                iii++;
            } else if (sx.equals("-1")&&iii>1){
                iii--;
            }
        }
        PageHelper.startPage(iii, 5);
        List<Bills> billsList = bs.qry(type2, d1, d2, a12, a22);
        PageInfo<Bills> billsPageInfo = new PageInfo<>(billsList);
        sess.setAttribute("pageinfo", billsPageInfo);
        sess.setAttribute("billList", billsList);
        return "mainlist";
    }

    @RequestMapping("/add")
    public String add(Bills b) {
        System.out.println(Thread.currentThread().getId() + "-" + LocalTime.now() + b);
        Integer add_count = bs.add(b);
        System.out.println(Thread.currentThread().getId() + "-" + LocalTime.now() + "已添加条数：" + add_count);
        return "redirect:index.jsp";
    }
}
